﻿using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Models;

namespace ZoneLine.Stations.HStation
{
    public abstract class HStationBase : WorkStationDBase
    {
        public void Init(string ip, int port = 102, string startAddr = "DB55.0", string endUploadCodeAddr = "DB55.18.1", string endUploadDataAddr = "DB55.18.2", string allowWorkAddr = "DB55.20")
        {
            Ip = ip;
            Port = port;
            _startAddr = startAddr;
            _readLength = 183;

            EndUpLoadCodeAddr = endUploadCodeAddr;
            EndUpLoadDataAddr = endUploadDataAddr;
            AllowWorkAddr = allowWorkAddr;
            HeartToPlcAddr = "DB55.18.0";

            StationRecordData = new HOP010StationResult();
        }

        private HStationResultBase _stationRecordData;

        public HStationResultBase StationRecordData
        {
            get { return _stationRecordData; }
            set => SetProperty(ref _stationRecordData, value);
        }

        protected override WorkActionEnum Validata(string stationName, string code, string[] otherStations = null)
        {
            var currentStationName = stationName;
            var stationIndex = Cache.WorkStationValidationList.IndexOf(currentStationName);
            if (stationIndex == -1)
            {
                return WorkActionEnum.NoEnable;
            }
            if (stationIndex > 0)
            {
                //检测上一个站
                var beforeStationName = Cache.WorkStationValidationList[stationIndex - 1];
                LogWithUi(_logUiChannel, $"在{currentStationName}工位上一个启动的检查工位是{beforeStationName}");
                var checkRlt = CheckCodeExistAndResult(beforeStationName, code);
                LogWithUi(_logUiChannel, $"在{beforeStationName}工位检查{code}的结论是{checkRlt}");
                if (checkRlt == 1)
                {
                    if (otherStations != null)
                    {
                        LogWithUi(_logUiChannel, $"在上个工位{beforeStationName}工位检查{code}的结论是{checkRlt},开始检查校验工位结论");
                        foreach (var otherStation in otherStations)
                        {
                            LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}是否工作");
                            var otherStationResult = CheckCodeExistAndResult(otherStation, code);
                            if (otherStationResult == 1)
                            {
                                LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}工作过，不在本工位进行工作");
                                return WorkActionEnum.WorkOkInCheckStation;
                            }
                        }
                    }
                    LogWithUi(_logUiChannel, $"在上个工位{beforeStationName}工位检查{code}的结论是{checkRlt},开始检查是否在本工位{currentStationName}结论");
                    var currentResult = CheckCodeExistAndResult(currentStationName, code);
                    LogWithUi(_logUiChannel, $"在本工位{currentStationName}工位检查{code}的结论是{currentResult}");
                    if (currentResult == 1)
                    {
                        return WorkActionEnum.HasWorkInCurrent;
                    }
                    else
                    {
                        return WorkActionEnum.StartWork;
                    }
                }
                else if (checkRlt == 2)
                {
                    return WorkActionEnum.WorkNgInBefore;
                }
                else
                {
                    return WorkActionEnum.NotWorkInBefore;
                }
            }
            else
            {
                //检查当前工位
                if (otherStations != null)
                {
                    LogWithUi(_logUiChannel, $"在{currentStationName}工位不存在上个工位，检查并行工位工作结论");

                    foreach (var otherStation in otherStations)
                    {
                        LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}是否工作");
                        var otherStationResult = CheckCodeExistAndResult(otherStation, code);
                        if (otherStationResult == 1)
                        {
                            LogWithUi(_logUiChannel, $"{currentStationName}检查校验工位{otherStation}工作过，不在本工位进行工作");
                            return WorkActionEnum.WorkOkInCheckStation;
                        }
                    }
                }
                LogWithUi(_logUiChannel, $"在{currentStationName}工位不存在上个工位，检查在本工位工作结论");
                var checkResult = CheckCodeExistAndResult(currentStationName, code);
                if (checkResult == 1)
                {
                    return WorkActionEnum.HasWorkInCurrent;

                }
                else
                {
                    return WorkActionEnum.StartWork;
                }
            }
        }
    }
}
